# Load libraries ----
library(tidyverse)
library(ggthemes)
library(multcomp)
library(gtsummary)
library(effects)
library(ggeffects)
library(gridExtra)
library(cowplot)
library(grid)
library(haven)
library(srvyr)
library(Hmisc)
library(rio)
library(modelsummary)
library(jtools)
library(xtable)
library(ggpubr)
library(rstatix)
library(interactions)
library(stevemisc)
library(lmtest)
library(sandwich)
library(estimatr)
library(dplyr)
library(kableExtra)
library(flextable)

options(scipen = 999) # Turn off scientific notation

# Define themes ----
mytheme <- function(...){
  theme_clean() +   
    theme(plot.title.position="panel",
          text=element_text(family="serif", color="gray15"), 
          axis.title=element_text(size=12, color="gray15"), 
          axis.text=element_text(size=12, color="gray15"),
          plot.title = element_text(size=12, color = "gray15"), 
          plot.caption=element_text(size = 12),  
          legend.position = "bottom",
          plot.background = element_rect(color="white"), 
          axis.title.y = element_text(margin = margin(t=0,r=10,b=0,l=0)), 
          legend.background = element_rect(color=NA), 
          axis.ticks = element_line(color="gray15"), 
          axis.line.x = element_line(color="gray15"), 
          axis.line.y = element_line(color="gray15"),
          panel.grid.major.y = element_line(color = "grey90", linetype = "solid"),
          legend.text = element_text(size = 16),
          strip.text.x = element_text(size = 18))
  }

mytheme2 <- function(...){
  theme_clean() +   
    theme(plot.title.position="panel",
          text=element_text(family="serif", color="gray15"), 
          axis.title=element_text(size=12, color="gray15"), 
          axis.text=element_text(size=12, color="gray15"),
          plot.title = element_text(size=12, color = "gray15"), 
          plot.caption=element_text(size = 12),  
          legend.position = "bottom",
          plot.background = element_rect(color="white"), 
          axis.title.y = element_blank(), 
          legend.background = element_rect(color=NA), 
          axis.ticks = element_line(color="gray15"), 
          axis.line.x = element_line(color="gray15"), 
          axis.line.y = element_line(color="gray15"),
          panel.grid.major.y = element_line(color = "grey90", linetype = "solid"),
          strip.text.x = element_text(size = 18),
          legend.text = element_text(size = 16))
  }


# Load in data ----
dso = import("Exp.2.data.csv")

ds = dso %>% 
  filter(consent == "I agree to participate", 
         attention =="Moderately interested",
         Finished == "TRUE",
         rid != '',
         debrief != "No, withdraw my responses from the study"
  ) %>% 
  mutate(project_assignment = case_when(FL_338_DO=="Exp_PP.Top_Amount.High" ~ "top.high",
                                        FL_338_DO=="Exp_PP.Top_Amount.Low" ~ "top.low",
                                        FL_338_DO=="Exp_PP.Six_Amount.High" ~ "six.high",
                                        FL_338_DO=="Exp_PP.Six_Amount.Low" ~ "six.low"),
         project_assignment = factor(project_assignment,
                                     levels=c('six.low','six.high','top.low','top.high')),
         priority_treatment = case_when(FL_338_DO=="Exp_PP.Top_Amount.High" ~ "Top",
                                        FL_338_DO=="Exp_PP.Top_Amount.Low" ~ "Top",
                                        FL_338_DO=="Exp_PP.Six_Amount.High" ~ "Six",
                                        FL_338_DO=="Exp_PP.Six_Amount.Low" ~ "Six"),
         priority_treatment = factor(priority_treatment,
                                     levels=c('Six','Top')),
         influence_treatment = case_when(FL_338_DO=="Exp_PP.Top_Amount.High" ~ "High",
                                         FL_338_DO=="Exp_PP.Top_Amount.Low" ~ "Low",
                                         FL_338_DO=="Exp_PP.Six_Amount.High" ~ "High",
                                         FL_338_DO=="Exp_PP.Six_Amount.Low" ~ "Low"),
         influence_treatment = factor(influence_treatment,
                                      levels=c('Low','High')),
         post_feel = case_when(post_feel_top.high_34!="" ~ post_feel_top.high_34,
                               post_feel_top.low_34!="" ~ post_feel_top.low_34,
                               post_feel_six.high_34!="" ~ post_feel_six.high_34,
                               post_feel_six.low_34!="" ~ post_feel_six.low_34),
         pre_feeling = feeling_grid_2,
         feeling_change =  post_feel - pre_feeling) 


# Manuscript Figure 6 ----

# Figure 6A
ds %>% 
  drop_na(feeling_change) %>%
  group_by(priority_treatment, influence_treatment) %>% 
  summarise(nrow = length(feeling_change), 
            mean = mean(feeling_change), 
            se = sd(feeling_change)/sqrt(nrow),
            ci = 1.96*se) %>% 
  ggplot(., aes(x = priority_treatment, y = mean, shape = influence_treatment)) + 
  geom_pointrange(aes(ymin = mean-ci, ymax = mean+ci), position = position_dodge(width = .5), size = 1, linewidth=1) + 
  labs(y = "Senator Feeling Therm. Change",
       title = "A) Mean Senator Support Change",
       x = "Project Demand",
       shape = "Senator Influence") +  
  scale_x_discrete(labels=c("Six" = "Low Priority", "Top" = "High Priority")) + 
  scale_shape_manual(values=c(19, 15), labels = c("Low", "High"))+
  mytheme() -> figure_6a

# Figure 6B
summary(feel_mod <- lm(feeling_change ~  
                         priority_treatment + influence_treatment 
                       , data = ds))
models_feel <- list()
models_feel[['Feeling Therm.']] <- feel_mod

data.frame(modelplot(models_feel, draw = FALSE)) -> feel.data.frame

feel.data.frame$dv <- "Feeling Therm. Change"

ds_feel_mods <- feel.data.frame %>% 
  filter(term %in% c("priority_treatmentTop", "influence_treatmentHigh")) %>% 
  mutate(term = case_when(term == "priority_treatmentTop" ~ "Project Demand",
                          term == "influence_treatmentHigh" ~ "Senator Influence"),
         term = factor(term, levels=c('Senator Influence','Project Demand')),)

ggplot(ds_feel_mods, aes(x = term, y = estimate)) + 
  geom_pointrange(aes(ymin = conf.low, ymax = conf.high), linewidth=1) + 
  labs(x = "Treatment Condition",
       y = "Coefficient",
       title = "B) Effect of Treatments on Senator Support Change"
  ) + 
  geom_hline(yintercept = 0, color='black', linetype = 'dashed')+
  coord_flip()+
  mytheme2()  -> figure_6b

# Combine Figures
plot_grid(figure_6a, figure_6b) -> figure_6
